﻿using System;
using System.Drawing;
using System.Windows.Forms;
using ImageFan.Controls;
using ImageFan.ImageHandling;

namespace ImageFan.Forms
{
    internal partial class ImageForm
        : Form
    {
        public ImageForm(ThumbnailsSequence thumbnailsSequence, ImageFile imageFile)
        {
            InitializeComponent();

            _thumbnailsSequence = thumbnailsSequence;

            _maxWidth = Screen.PrimaryScreen.Bounds.Size.Width - FormScreenBorders;
            _maxHeight = Screen.PrimaryScreen.Bounds.Size.Height - FormScreenBorders;

            MaximumSize = new Size(_maxWidth, _maxHeight);

            ImageSetUp(imageFile);
        }

        public void ImageSetUp(ImageFile imageFile)
        {
            var previousImageFile = _imageFile;
            
            _imageFile = imageFile;

            _image = _imageFile.ImageFromDisc;

            Text = _imageFile.FileName;
            pbImage.Image = _image;
            pbImage.Size = _image.Size;

            ClientSize = new Size(Math.Min(pbImage.Size.Width, _maxWidth),
                                  Math.Min(pbImage.Size.Height, _maxHeight));

            if ((HorizontalScroll.Visible) && (Width + ScrollDistance <= _maxWidth))
                Width += ScrollDistance;
            if ((VerticalScroll.Visible) && (Height + ScrollDistance <= _maxHeight))
                Height += ScrollDistance;

            CenterToScreen();

            if ((previousImageFile != null) && (previousImageFile != _imageFile))
                previousImageFile.DisposeImageFromDisc();
        }

        #region Private

        private const int FormScreenBorders = 12;
        private const int ScrollDistance = 18;
        
        private ThumbnailsSequence _thumbnailsSequence;
        private ImageFile _imageFile;
        private Image _image;

        private int _maxWidth;
        private int _maxHeight;
        
        private void ImageForm_FormClosed(object sender, FormClosedEventArgs args)
        {
            pbImage.Image = GlobalData.InvalidImage;

            _imageFile.DisposeImageFromDisc();

            _thumbnailsSequence.SetPendingAction();
            GlobalData.MainFormInstance.Activate();
        }

        private void ImageForm_KeyDown(object sender, KeyEventArgs args)
        {
            if (args.KeyCode == Keys.Enter)
            {
                ShowFullScreenImageForm();
            }
            else if ((args.KeyCode == Keys.Space) || (args.KeyCode == Keys.Down) || (args.KeyCode == Keys.Right) ||
                     (args.KeyCode == Keys.PageDown) || (args.KeyCode == Keys.S) || (args.KeyCode == Keys.D))
            {
                if (_thumbnailsSequence.NextThumbnail != null)
                {
                    _thumbnailsSequence.MoveToNextThumbnail();
                    ImageSetUp(_thumbnailsSequence.CurrentThumbnail.ImageFile);
                }
            }

            else if ((args.KeyCode == Keys.Back) || (args.KeyCode == Keys.Up) || (args.KeyCode == Keys.Left) ||
                     (args.KeyCode == Keys.PageUp) || (args.KeyCode == Keys.W) || (args.KeyCode == Keys.A))
            {
                if (_thumbnailsSequence.PreviousThumbnail != null)
                {
                    _thumbnailsSequence.MoveToPreviousThumbnail();
                    ImageSetUp(_thumbnailsSequence.CurrentThumbnail.ImageFile);
                }
            }

            else if (args.KeyCode == Keys.Escape)
                Close();
        }

        private void pbImage_MouseClick(object sender, MouseEventArgs args)
        {
            if (args.Button == MouseButtons.Left)
                ShowFullScreenImageForm();
        }

        private void ShowFullScreenImageForm()
        {
            var fullScreenImage = new FullScreenImage(this, _thumbnailsSequence, _imageFile);
            fullScreenImage.ShowDialog();
        }

        #endregion
    }
}
